import axios from 'axios';
import { Message, MessageBox, Notification } from 'element-ui';
import store from '../store'
import { baseUrl } from './baseUrl';

// const service = axios.create({
//     baseURL: 'http://mis.jfzh.com.cn:8018/api/',
//     headers: {
//         post: {
//             "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
//         }
//     },
//     timeout: 10000,
// })
// //请求拦截
// service.interceptors.request.use(
//     config => {
//         //添加token
//         config.headers.Token = sessionStorage.getItem('Token')
//         return config 
//     },
//     error => Promise.error(error)
// )
// //错误状态封装
// const errorHandle = status => {
//     switch (status) {
//         case 0:
//             Message.error('系统错误，请联系管理员')
//             break;
    
//         default:
//             break;
//     }
// }
// //返回拦截
// service.interceptors.response.use(
//     response => {
//         // console.log(response)
//         if (response.status === 200) {
//             return Promise.resolve(response.data)
//         } else {
//             return Promise.reject(response.data)
//         }
//     },
//     error => {
//         const { response } = error
//         if (response) {
//             errorHandle(response.status, response.data.msg)
//             return Promise.reject(response)
//         }
//     }
        
// )
// export default service
const service = axios.create({
    baseURL: baseUrl,
    headers: {
        post: {
            "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
        }
    },
    //代理业务-签约列表接口响应时间超过原来的10000
    timeout: 50000,
})
//请求拦截  
service.interceptors.request.use(
    config => {    
        //添加token
        config.headers.Token = sessionStorage.getItem('Token')
        config.headers.Platform = "vXBrUZ7rWE5D4P4ENnwR6GFvaG2pVgul"
        return config
    },
    error => Promise.error(error)
)

//返回拦截
service.interceptors.response.use(
    response => {   
        let code = response.data.code
        if (code === 40100 || code === 40101 || code === 40102) {
            MessageBox.confirm(`用户${response.data.msg}，请重新登录`, '系统提示', {
                confirmButtonText: '去登录',
                cancelButtonText: '取消',
                type: 'warning'
            }).then((res) => {
                if (res === 'confirm') {
                    store.dispatch('LogOut').then(() => {
                        location.href = "/index"
                    })
                }
            }).catch(() => {
                Message.info('取消登录')
            });
        } else if (code === 40600) {
            return Promise.resolve(response.data)
        } else if (code !== 200) {
            Notification.error({
                title: response.data.msg
            })
            return Promise.reject(response.data)
        } else {
            return Promise.resolve(response.data)
        }
    },
    error => {
        let { message } = error
        if (message == "Network Error") {
            message = "后端接口连接异常"
        } else if (message.includes('timeout')) {
            message = "系统接口请求超时"
        }
        Message({
            message: message,
            type: 'error',
            duration: 5 * 1000
        })
        return Promise.reject(error)
    }

)
export default service